<template>
  <!-- 模具寿命邮件发送记录主表表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="1400px"
    draggable
    @close="closeDialog"
  >
    <div
      style="display: flex; justify-content: space-between; align-items: center"
    >
      <h4 v-show="props.isReadonly">返工详情</h4>
    </div>
    <div>
      <el-table
        v-if="formData.reworks && formData.reworks.length > 0"
        :data="formData.reworks"
        style="width: 100%"
        :key="Math.random"
        max-height="240px"
        overflow-y="auto"
        border
      >
        <el-table-column type="index" label="序号" align="center" width="80" />

        <el-table-column label="返工编号" props="reworkCode" min-width="100">
          <template #default="scope">
            <el-input
              v-model="scope.row.reworkCode"
              placeholder="请输入返工编号"
              clearable
              :disabled="true"
              style="width: 195px"
              v-if="!props.isReadonly"
            />
            <span v-else>{{ scope.row.reworkCode }}</span>
          </template>
        </el-table-column>
        <el-table-column label="工序" props="process" min-width="80">
          <template #default="scope">
            <el-select
              style="width: 195px"
              v-model="scope.row.process"
              placeholder="请选择工序"
              filterable
              clearable
              v-if="!props.isReadonly"
            >
              <el-option
                v-for="item in useDictionary('ProcessType')"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
            <span v-else>{{
              useDictionary("ProcessType", scope.row.process)
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="返工状态" props="reworkStatus" min-width="80">
          <template #default="scope">
            <el-select
              style="width: 195px"
              v-model="scope.row.reworkStatus"
              placeholder="请选择返工状态"
              filterable
              clearable
              v-if="!props.isReadonly"
            >
              <el-option
                v-for="item in useDictionary('reworkOrderStatus')"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
            <span v-else>{{
              useDictionary("reworkOrderStatus", scope.row.reworkStatus)
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="返工数量" props="reworkNum" min-width="80">
          <template #default="scope">
            <el-input
              v-model="scope.row.reworkNum"
              placeholder="请输入返工数量"
              clearable
              :disabled="true"
              style="width: 195px"
              v-if="!props.isReadonly"
            />
            <span v-else>{{ scope.row.reworkNum }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="返工时间"
          props="reworkTime"
          min-width="100"
          align="center"
        >
          <template #default="scope">
            <el-input
              v-model="scope.row.reworkTime"
              placeholder="请输入返工时间"
              clearable
              :disabled="true"
              style="width: 195px"
              v-if="!props.isReadonly"
            />
            <span v-else>{{ scope.row.reworkTime }}</span>
          </template>
        </el-table-column>

        <!-- <el-table-column
          fixed="right"
          align="center"
          label="操作"
          width="100"
          v-if="!props.isReadonly"
        >
          <template #default="scope">
            <el-button
              type="danger"
              link
              @click.stop="handleDelete(scope.$index)"
            >
              <i-ep-delete />
              删除
            </el-button>
          </template>
        </el-table-column> -->
      </el-table>
    </div>

    <div
      style="display: flex; justify-content: space-between; align-items: center"
    >
      <h4 v-show="props.isReadonly">履历附件</h4>
    </div>
    <div v-if="formData.files && formData.files.length">
      <CommonUploadFile
        :showText="false"
        :isRead="props.isReadonly"
        :files="formData.files"
        @update-files="updateFormFiles"
      />
    </div>
    <div v-else>无</div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closeDialog">关 闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
import { useDictionary } from "@/utils/useTable";
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "QMSQMSABNORMALORDERADDORUPDATEFILE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  judgmentRemark: [{ required: true, message: "请填写意见", trigger: "blur" }],
});

const formRef = ref(ElForm);

let formData: any = reactive({});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      Object.assign(formData, newVal);
      console.log("formData", formData);

      console.log("files", formData.files);
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formData.value = {};
}

const loading = ref(false);

/** 模具寿命邮件发送记录主表保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/eam/eammouldemailrecord/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

function noHandleSubmitRecord() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      formData.status = "3";
      await request({
        url: "/eam/eammouldemailrecord/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

function handleSubmitRecord() {
  if (!formData.files || formData.files.length == 0) {
    ElMessage.error("请上传至少一张图片");
    return;
  }
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      formData.status = "2";
      await request({
        url: "/eam/eammouldemailrecord/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});

function updateFormFiles(val: []) {
  formData.files = val;
}
</script>
